package com.zhangxingyu.util;

import com.zhangxingyu.domain.Pokemon;
import com.zhangxingyu.service.ManagerService;
import com.zhangxingyu.service.impl.ManagerServiceimpl;

import java.util.List;

/**
 * 这里是与升级有关的工具类，但是其静态方法已经基本被替代，静态成员变量还在使用，此处保留其方法用于描述与升级有关的做法
 */
public class UpgradeUtils {
    public static double power=3;
    public static double[] baseExp={2,3,4};//不同稀有度的宠物对应的基础经验值：普通-->>2，稀有-->>3，史诗-->>4
    public static int[] baseAttribute={2,4,6};//不同稀有度的宠物对应的基础属性值：普通-->>2，稀有-->>4，史诗-->>6
    public static double multiplier=1.5;
    public static ManagerService managerService =new ManagerServiceimpl();
    /**
     *
     * @param pokemon
     * @return 该方法获取宠物升级所需要的经验（经验槽），不同稀有度的宠物的基础经验值不同，越稀有的宠物经验基础值越大，经验槽增长越快
     *          即升级所需的经验增长越快，用于平衡游戏，不同稀有度与基础经验值的对应关系：普通-->>2，稀有-->>3，史诗-->>4
     *          计算公式为：基础经验值 * 宠物当前等级 ^ power
     */
    public static int getExpSlot(Pokemon pokemon)
    {
        if(pokemon.getRarity().equals("普通"))
            return (int) (baseExp[0]*Math.pow(pokemon.getGrade(),power));
        else if(pokemon.getRarity().equals("稀有"))
            return (int) (baseExp[1]*Math.pow(pokemon.getGrade(),power));
        else return (int) (baseExp[2]*Math.pow(pokemon.getGrade(),power));
    }

    /**
     *
     * @param pokemon
     * @return 该方法设置宠物升级或者重置后的血量和技能伤害，不同稀有度的宠物的基础属性值不同，越稀有的宠物基础属性值越大，
     *          血量和技能伤害增长越快，不同稀有度与基础属性值的对应关系：普通-->>10，稀有-->>10，史诗-->>30
     *          血量计算公式为：基础血量 + 3 *（等级-1） + 基础属性值 * （等级-1）
     *          技能伤害计算公式为：基础技能伤害 + 等级-1 + 基础属性值 / 2 *（等级-1）
     *          其中的基础血量值和基础技能伤害由管理员创建该宠物的时候设置
     */
    public static void setBloodAndAttack(Pokemon pokemon)
    {
        Pokemon pokemonFromTable = managerService.queryOnePokemonByname(pokemon.getName());
        if(pokemon.getRarity().equals("普通"))
        {
            pokemon.setBlood(pokemonFromTable.getBlood()+3*(pokemon.getGrade()-1)+baseAttribute[0]*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_1(pokemonFromTable.getSkillvalue_1()+pokemon.getGrade()-1+baseAttribute[0]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_2(pokemonFromTable.getSkillvalue_2()+pokemon.getGrade()-1+baseAttribute[0]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_3(pokemonFromTable.getSkillvalue_3()+pokemon.getGrade()-1+baseAttribute[0]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_4(pokemonFromTable.getSkillvalue_4()+pokemon.getGrade()-1+baseAttribute[0]/2*(pokemon.getGrade()-1));

        }else if(pokemon.getRarity().equals("稀有"))
        {
            pokemon.setBlood(pokemonFromTable.getBlood()+3*(pokemon.getGrade()-1)+baseAttribute[1]*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_1(pokemonFromTable.getSkillvalue_1()+pokemon.getGrade()-1+baseAttribute[1]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_2(pokemonFromTable.getSkillvalue_2()+pokemon.getGrade()-1+baseAttribute[1]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_3(pokemonFromTable.getSkillvalue_3()+pokemon.getGrade()-1+baseAttribute[1]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_4(pokemonFromTable.getSkillvalue_4()+pokemon.getGrade()-1+baseAttribute[1]/2*(pokemon.getGrade()-1));
        }else{
            pokemon.setBlood(pokemonFromTable.getBlood()+3*(pokemon.getGrade()-1)+baseAttribute[2]*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_1(pokemonFromTable.getSkillvalue_1()+pokemon.getGrade()-1+baseAttribute[2]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_2(pokemonFromTable.getSkillvalue_2()+pokemon.getGrade()-1+baseAttribute[2]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_3(pokemonFromTable.getSkillvalue_3()+pokemon.getGrade()-1+baseAttribute[2]/2*(pokemon.getGrade()-1));
            pokemon.setSkillvalue_4(pokemonFromTable.getSkillvalue_4()+pokemon.getGrade()-1+baseAttribute[2]/2*(pokemon.getGrade()-1));
        }
    }
    public static void updateAttribute(List<Pokemon> opponent,List<Pokemon> participate)
    {
        int totalexp=0;
        for (int i = 0; i <opponent.size(); i++) {
            totalexp+=opponent.get(i).getExpSlot()*multiplier;
        }
        int avgexp=totalexp/participate.size();
        for (int i = 0; i < participate.size(); i++) {
            Pokemon pokemon = participate.get(i);
            pokemon.setExperience(pokemon.getExperience()+avgexp);
            while (pokemon.getExperience()>=pokemon.getExpSlot())
            {
                pokemon.setExperience(pokemon.getExperience()-pokemon.getExpSlot());
                pokemon.setGrade(pokemon.getGrade()+1);
                pokemon.setExpSlot(getExpSlot(pokemon));
            }
            setBloodAndAttack(pokemon);
        }
    }
}

